from functools import wraps
from fastapi import Request
from typing import List


def RequiresPermissions(scopes: List[str]):
    """
    权限装饰器
    :param scopes: 功能权限
    :return:
    """
    def decorator(func):
        @wraps(func)
        async def wrapper(*args, **kwargs):
            req: Request = kwargs.get('req')
            
            print("需要操作权限:", scopes)
            # raise HTTPException(status_code=401, detail="无访问权限")
            result = await func(*args, **kwargs)

            return result
        return wrapper

    return decorator


def RequiresRoles(roles: List[str]):
    """
    权限装饰器
    :param scopes: 功能权限
    :param roles: 角色
    :return:
    """

    def decorator(func):
        @wraps(func)
        async def wrapper(*args, **kwargs):
            req: Request = kwargs.get('req')

            print("需要角色:", roles)
            # raise HTTPException(status_code=401, detail="无访问权限")
            result = await func(*args, **kwargs)

            return result

        return wrapper

    return decorator
